﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
using DTO;
namespace DAO
{
    public class DAO_ChuyenNganh
    {
        public static List<DTO_ChuyenNganh> SelectChuyenNganh()
        {
            List<DTO_ChuyenNganh> listChuyenNganh = new List<DTO_ChuyenNganh>();
            try
            {
                string s = "Proc_selectChuyenNganh";
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = ketnoi.Cmb();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = s;
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = cmd;
                DataTable dt = new DataTable();
                da.Fill(dt);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    DTO_ChuyenNganh chuyennganh = new DTO_ChuyenNganh();
                    chuyennganh.MaCN = dt.Rows[i]["MaCN"].ToString();
                    chuyennganh.TenCN = dt.Rows[i]["TenCN"].ToString();
                    listChuyenNganh.Add(chuyennganh);
                }
                ketnoi.Cmb().Close();
                return listChuyenNganh;
            }
            catch (Exception ex)
            {
                return listChuyenNganh;
            }
        }
        public static bool deleteChuyenNganh(string macn)
        {
            List<DTO_ChuyenNganh> listChuyenNganh = new List<DTO_ChuyenNganh>();
            // lay danh sach Khoa hoc theo Ma Khoa
            try
            {
                string s = "Proc_deleteChuyenNganh";
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = ketnoi.Cmb();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = s;
                cmd.Parameters.Add("@MaCN", SqlDbType.NVarChar).Value = macn;
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = cmd;
                DataTable sv = new DataTable();
                da.Fill(sv);
                for (int i = 0; i < sv.Rows.Count; i++)
                {
                    DTO_ChuyenNganh chuyennganh = new DTO_ChuyenNganh();
                    chuyennganh.MaCN = sv.Rows[i]["MaCN"].ToString();
                    listChuyenNganh.Add(chuyennganh);
                }
            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }
        public static bool updateChuyennganh(DTO_ChuyenNganh chuyennganh, string macn_cu)
        {
            try
            {
                SqlConnection cn = ketnoi.Cmb();
                string s = "Proc_UpdateChuyenNganh";
                SqlCommand cmd1 = new SqlCommand(s, cn);
                cmd1.CommandType = CommandType.StoredProcedure;
                cmd1.Parameters.AddWithValue("@MaCN", chuyennganh.MaCN);
                cmd1.Parameters.AddWithValue("@TenCN", chuyennganh.TenCN);
                cmd1.Parameters.AddWithValue("@MaCN_Cu", macn_cu);
                if (cn.State == ConnectionState.Closed)
                    cn.Open();
                cmd1.ExecuteNonQuery();

            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }
        public static bool insertChuyenNganh(DTO_ChuyenNganh chuyennganh)
        {
            try
            {
                SqlConnection cn = ketnoi.Cmb();
                string s = "Proc_InsertChuyenNganh";
                SqlCommand cmd1 = new SqlCommand(s, cn);
                cmd1.CommandType = CommandType.StoredProcedure;
                cmd1.Parameters.AddWithValue("@MaCN", chuyennganh.MaCN);
                cmd1.Parameters.AddWithValue("@TenCN", chuyennganh.TenCN);
                if (cn.State == ConnectionState.Closed)
                    cn.Open();
                cmd1.ExecuteNonQuery();

            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }
    }
}